package com.jiang.droidsms.ui;

import com.jiang.droidsms.R;
import com.jiang.droidsms.data.DroidSMSConfig;
import com.jiang.droidsms.data.SMSTable.SMS;
import com.jiang.droidsms.ui.adapter.ConversationListAdapter;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * The Class ConversationActivity.
 */
public class ConversationActivity extends Activity {

	/** The m list view. */
	private ListView mListView;

	/** The m adapter. */
	private ListAdapter mAdapter;

	/** The m address. */
	private String mAddress;

	/** The m resolver. */
	private ContentResolver mResolver;

	/** The m thread id. */
	private int mThreadId;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversation_layout);
		mListView = (ListView) findViewById(R.id.list);
		mListView.setStackFromBottom(true);
		mListView.setBackgroundColor(0xffffffff);
		mListView.setCacheColorHint(0xffffffff);
		mListView.setDividerHeight(0);

		mResolver = getContentResolver();
		mThreadId = getIntent().getIntExtra(DroidSMSConfig.EXTRA_THREAD_ID, 0);
		final Cursor cursor = mResolver.query(SMS.CONTENT_URI, null,
				SMS.THREAD_ID + "=" + mThreadId + " AND " + SMS.TYPE + " != "
						+ SMS.TYPE_SMS_DRAFT, null, SMS.DATE + " ASC");
		cursor.moveToFirst();
		mAddress = cursor.getString(cursor.getColumnIndex(SMS.ADDRESS));
		mAdapter = new ConversationListAdapter(this, cursor);
		mListView.setAdapter(mAdapter);

		LinearLayout sendLayout = (LinearLayout) findViewById(R.id.send_bar);
		sendLayout.setFocusable(true);
		sendLayout.setFocusableInTouchMode(true);
		sendLayout.requestFocus();
		
		ContentValues readValues = new ContentValues();
		readValues.put(SMS.READ, 1);
		mResolver.update(SMS.CONTENT_URI, readValues, SMS.READ + "=" + 0, null);

		Button sendButton = (Button) sendLayout.findViewById(R.id.send);
		
		final EditText smsContent = (EditText) sendLayout
		.findViewById(R.id.input);
		sendButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String body = smsContent.getText().toString();
				if (!TextUtils.isEmpty(body)) {
					SmsManager.getDefault().sendTextMessage(mAddress, null,
							body, null, null);

					/** Write the SMS to system database. */
					ContentValues values = new ContentValues();
					values.put(SMS.BODY, body);
					values.put(SMS.THREAD_ID, mThreadId);
					values.put(SMS.DATE, System.currentTimeMillis());
					values.put(SMS.ADDRESS, mAddress);
					values.put(SMS.READ, 1);
					values.put(SMS.TYPE, SMS.TYPE_SMS_OUT);
					values.put(SMS.PROTOCOL, 0);
					mResolver.insert(SMS.CONTENT_URI, values);
					smsContent.setText("");
				}
			}
		});
	}
}
